var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		info:'',
		area:'',
		address: '',
		longitude: '',
		latitude: '',
		regiondata:'',
		type:0,
		addressxx:'',
	},
	_onLoad: function () {
		var that = this;
		var addressId = that.options.id
		that.setData({type:that.options.type})
		if (addressId) {
			$.get('address/addressadd', { id: addressId,type:that.options.type}, function (res) {
				that.setData(res)
				if(res.info.province){
					var regiondata = [res.info.province,res.info.city,res.info.district]
				}else{
					var regiondata = '';
				}
				that.setData({regiondata:regiondata})
			})
		}
	},
	selectzuobiao:function(){
		var that = this
		wx.chooseLocation({success:function(res){
			console.log(res)
			that.setData({
				'info.area':res.address,
				'info.address':res.name,
				'info.latitude':res.latitude,
				'info.longitude':res.longitude
			})
		},fail:function(res){
			if(res.errMsg == 'chooseLocation:fail auth deny'){
				//$.error('获取位置失败，请在设置中开启位置信息');
				$.confirm('获取位置失败，请在设置中开启位置信息',function(){
					wx.openSetting({});
				})
			}
		}})	
	},
	formSubmit: function (e) {
		var that = this;
		var formdata = e.detail.value
		var addressId = that.options.id
		var name = formdata.name
		var tel = formdata.tel
		var regiondata = that.data.regiondata
		if(that.options.type == 1){
			var area = formdata.area
		}else{
			var area = regiondata.join(' ');
		}
		var address = formdata.address
		var longitude = that.data.info.longitude
		var latitude = that.data.info.latitude
		if (name == '' || tel == '' || address == '') {
			$.error('请填写完整信息');
			return;
		}
    $.post('address/addressadd', {type:that.options.type,addressid:addressId,name:name,tel:tel,area:area,address:address,latitude:latitude,longitude:longitude}, function (res) {
			if(res.status == 0){
				$.alert(res.msg);
				return;
			}
			$.success('保存成功');
			setTimeout(function () {
				$.goback(true);
			}, 1000)
		}, '提交中');
	},
	delAddress: function () {
		var that = this;
		var addressId = that.options.id
		$.confirm('确定要删除该收获地址吗?', function () {
			$.post('address/del', {addressid: addressId}, function () {
				$.success('删除成功');
				setTimeout(function () { //刷新并返回上一个页面
					$.goback(true);
				}, 1000)
			}, '提交中')
		})
	},
	bindPickerChange: function (e) {
		var val = e.detail.value;
		this.setData({ regiondata: val })
	},
	setaddressxx:function(e){
		this.setData({addressxx:e.detail.value})
	},
	shibie:function(){
		var that = this;
		var addressxx = that.data.addressxx
		$.post('address/shibie', {addressxx:addressxx}, function (res) {
			var isrs = 0;
			if(res.province){
				isrs = 1;
				that.setData({regiondata:[res.province,res.city,res.county]});
			}
			if(res.detail){
				isrs = 1;
				that.setData({'info.address':res.detail});
			}
			if(res.person){
				isrs = 1;
				that.setData({'info.name':res.person});
			}
			if(res.phonenum){
				isrs = 1;
				that.setData({'info.tel':res.phonenum});
			}
			if(isrs == 0){
				$.error('识别失败');
			}else{
				$.error('识别完成');
			}
		})
	}
}))